import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/api/festival.dart';
import 'package:geomancy/models/festival_article_list_model.dart';
import 'package:geomancy/styles/index.dart';
import 'package:geomancy/tools/constant.dart';
import 'package:geomancy/widgets/common_header.dart';
import 'package:geomancy/widgets/common_loading_circle.dart';
import 'package:geomancy/widgets/common_text.dart';

class CalendarFestivalArticlePage extends StatefulWidget {
  final Article article;
  const CalendarFestivalArticlePage({super.key, required this.article});

  @override
  State<CalendarFestivalArticlePage> createState() => _CalendarFestivalArticlePageState();
}

class _CalendarFestivalArticlePageState extends State<CalendarFestivalArticlePage> {
  String? _htmlContent;
  bool loading = false;

  @override
  void initState() {
    super.initState();
    getArticleDetail();
  }

  Future getArticleDetail() async {
    try {
      setState(() {
        loading = true;
      });
      final result = await FestivalAPI.getArticleDetail(context, widget.article.url!);
      setState(() {
        _htmlContent = result;
      });
    } finally {
      setState(() {
        loading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonHeader.appBar(context: context, title: widget.article.title),
      backgroundColor: ColorStyle.pageDefaultColor,
      body: ListView(
        physics: const ClampingScrollPhysics(),
        padding: EdgeInsets.fromLTRB(Constant.PAGEPADDING.w, 20.w, Constant.PAGEPADDING.w, 40.w),
        children: [
          Container(
            padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.w),
            decoration: BoxDecoration(
              color: ColorStyle.themeLightColor,
              border: Border.all(width: 5.w, color: ColorStyle.borderColor),
              borderRadius: BorderRadius.all(Radius.circular(60.w))
            ),
            child: loading ? Padding(
              padding: EdgeInsets.all(50.w),
              child: const CommonLoadingCircleWidget(),
            ) : _htmlContent != null ? Html(
              data: """
                $_htmlContent
                """,
              style: {
                'body': Style(
                  fontSize: FontSize(30.sp), // 设置全局字体大小
                  color: ColorStyle.textColor, // 设置字体颜色
                ),
              },
            ) : Container(height: 200.w, alignment: Alignment.center, child: const CommonText(txt: '暂无数据!',),),
          )
        ],
      ),
    );
  }
}
